<?php
$this->breadcrumbs = array(
	'费用报销单' => array(
		'listExpense'
	),
	'查看报销单',
);

$this->userActions = array(
	array(
		'label' => '费用报销单',
		'url' => array(
			'listExpense'
		)
	),
	array(
		'label' => '生成报销单',
		'url' => array(
			'generateExpense'
		)
	),
);

$numberFormatter = Yii::app()->numberFormatter;
$formatter = Yii::app()->format;
$codeManager = Yii::app()->codeManager;
$attached = $expense->isAttached();
?>
<div class="material-expense">
<div class="span-4"><?php if ($expense->getIsNewRecord()) { ?>
预览，不可用于报销
<?php }elseif($attached){
	?> 已报销存档<br /><?php 
} ?>&nbsp;</div>
<div class="material-expense-title span-9">
材料报销单
</div><div class="span-4 last" style="text-align: right;">
<?php if (!$expense->getIsNewRecord()) {
	if($attached){
if($expense->accountTokenId){
	echo $expense->project->name.'<br />'.$expense->accountToken->tokenYear.'-'
	.$expense->accountToken->tokenMonth.' #'.$expense->accountToken->number;
}else{
	echo '凭证号：'.$expense->externalTokenDate.'#'.$expense->externalTokenNumber;
}
?>
		<?php
	}else{
		$codeManager->barCode($expense);
	}
} else {
?>
预览，不可用于报销
<?php } ?>
</div>
<div class="clear"></div>

<div class="material-expense-meta">
<div class="span-2 label">
项目:
</div>
<div class="span-5">
<?php echo $expense->project->name; ?>
</div>
<div class="span-2 label">
报销日期:
</div>
<div class="span-2">
<?php echo $expense->expenseDate; ?>
</div>
<div class="span-2 label">
打印时间:
</div>
<div class="span-4 last">
<?php echo date('Y-m-d h:m:s'); ?>
</div>
<div class="clear"></div>
<?php if ($expense->createUserId) {
	$createUser = $expense->createUser;
	$createTime = $expense->createTime;
} else {
	$createUser = AccessControl::getCurrentUser();
	$createTime = date('Y-m-d h:m:s');
}
?>
<div class="span-2 label">
制作:
</div>
<div class="span-9">
<?php echo $createUser->employee->fullName; ?>
</div>
<div class="span-2 label">
制单时间:
</div>
<div class="span-4 last">
<?php echo $createTime; ?>
</div>
<div class="clear"></div>
<?php if ($expense->note) { ?>
<div class="span-2 label">
备注:
</div>
<div class="span-15 last">
<?php echo $expense->note; ?>
</div>
<div class="clear"></div>
<?php } ?>
</div>

<div class="material-expense-receipts">
<?php
$receipts = $expense->getGenerateReceipts();
$totalPrice = 0.0;
$projectId = null;
?>
<?php foreach ($receipts as $receipt) {
	$itemTotalPrice = $receipt->getTotalPrice();
	if (!$projectId) {
		$projectId = $receipt->projectId;
	}
?>
<div class="material-expense-receipt-item">
<div class="span-13">
<div class="span-2"><?php echo CHtml::link($receipt->receiptDate,
							array(
								'materialReceipt/view',
								'id' => $receipt->id
							), array(
								'target' => '_blank'
							));
					?></div>
<div class="span-6"><?php echo CHtml::encode($receipt->project->name); ?></div>
<div class="span-3">
	经办:
	<?php echo CHtml::encode($receipt->handler); ?>
</div>
<div class="span-2 last">
	<?php echo CHtml::encode($receipt->getAttributeLabel('type')); ?>:
	<?php echo CHtml::encode($receipt->displayType); ?>
</div>
<div class="clear"></div>

<div class="span-8">
	<?php echo CHtml::encode($receipt->getAttributeLabel('merchantId')); ?>:
	<?php if ($receipt->merchant) {
			echo CHtml::encode($receipt->merchant->name);
		}
	?>
</div>
<div class="span-3">
	制票:
	<?php echo CHtml::encode($receipt->createUser->employee->fullName); ?>
</div>
<div class="span-2 last">
	<?php echo CHtml::encode($receipt->getAttributeLabel('warehouseManagerId')); ?>:
	<?php echo CHtml::encode($receipt->warehouseManager->fullName); ?>
</div>
<div class="clear"></div>
</div>
<div class="span-4 last" style="text-align: right;">
<?php
	if (!$expense->getIsNewRecord()&&!$attached) {
		$codeManager->barCode($receipt);
	}
?>
</div>
<div class="clear"></div>
<?php if ($receipt->note) { ?>
<div><?php echo CHtml::encode($receipt->getAttributeLabel('note')); ?>:
	<?php echo CHtml::encode($receipt->note); ?></div>
<?php } ?>
<?php
	$totalPrice += $itemTotalPrice;
?>

<div class="span-2">
	<?php echo $receipt->itemCount; ?> 件物品:
</div>

<div class="span-3">
<?php echo Yii::app()->format->formatCurrency($receipt->itemTotal); ?></div>
<div class="span-6">加工费/运费/其它费用合计:
	<?php echo Yii::app()->format
			->formatCurrency(
				$receipt->processingCost + $receipt->transportationCost + $receipt->otherCost);
	?></div>
<div class="span-3"><?php echo CHtml::encode($receipt->getAttributeLabel('discount')); ?>:
	<?php echo Yii::app()->format->formatCurrency($receipt->discount); ?></div>
<div class="span-3 last"><?php echo '总计'; ?>:
	<?php echo Yii::app()->format->formatCurrency($itemTotalPrice); ?></div>
<div class="clear"></div>
</div>
<?php
}
?>
</div>

<div class="material-expense-meta-footer">
<div class="span-14">
总金额：<?php echo $formatter->formatCurrency($totalPrice); ?>

(<?php echo $formatter->formatCapitalCurrency($totalPrice); ?>)
</div>
<div class="span-3 last" style="text-align: right;">
共 <?php echo count($receipts); ?> 张票据
</div>
<div class="clear"></div>
</div>
</div>
